﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using System.Web.WebSockets;
namespace z.Controllers {
	[z.Filters.InitializeSimpleMembership]
	public class HomeController : Controller {
		public ActionResult Index() {
			using (m.db.Database db = new m.db.Database()) {
				db.Database.CreateIfNotExists();
				if (db.UserProfiles.Count() == 0) {
					WebSecurity.CreateUserAndAccount("admin", "123");
					WebSecurity.Login("admin", "123", false);
					var role = new m.db.webpages_Roles();
					role.RoleName = "Admin";
					db.webpages_Roles.Add(role);
					var user = db.UserProfiles.Where(x => x.UserId == 1).Single();
					user.First_Name = "xxxxx";
					user.Last_Name = "xxxxx";
					user.Address = "xxxxxxx";
					user.Phone = "000000000";
					role.UserProfiles.Add(user);

					WebSecurity.CreateUserAndAccount("mod", "123");
					role = new m.db.webpages_Roles();
					role.RoleName = "Mod";
					db.webpages_Roles.Add(role);
					user = db.UserProfiles.Where(x => x.UserId == 2).Single();
					user.First_Name = "xxxxx";
					user.Last_Name = "xxxxx";
					user.Address = "xxxxxxx";
					user.Phone = "000000000";
					role.UserProfiles.Add(user);

					WebSecurity.CreateUserAndAccount("user", "123");
					user = db.UserProfiles.Where(x => x.UserId == 3).Single();
					user.First_Name = "xxxxx";
					user.Last_Name = "xxxxx";
					user.Address = "xxxxxxx";
					user.Phone = "000000000";
					db.SaveChanges();
					return RedirectToAction("Index", "Install");
				} else {
					return RedirectToAction("Index", "Occasion");
				}
			}
		}
		public ActionResult RealTime(string returnUrl) {
			if (Session["realtime"] == null)
				Session["realtime"] = false;
			if ((bool)Session["realtime"] == false) {
				Session["realtime"] = true;
			} else {
				Session["realtime"] = false;
			}
			return Redirect(returnUrl);
		}
		public ActionResult Error() {
			return View("~/Views/Home/Error.aspx");
		}
		Random rand = new Random();
		public ActionResult Captcha(string str) {
			Session["captcha"]=rand.Next().ToString("X");
			Response.ContentType = "image/gif";
			Bitmap bm = new Bitmap(400, 100);
			System.Drawing.Graphics grap = Graphics.FromImage(bm);
			Brush br = Brushes.Black;
			Font font = new Font("Arial", 50, GraphicsUnit.Pixel);
			grap.Clear(Color.White);
			grap.DrawString((string)Session["captcha"], font, br, layoutRectangle: new RectangleF(50, 25, 400, 100));
			Pen p=Pens.Red;
			for (int i = 0; i < 200; i++) {
				grap.DrawLine(new Pen(Color.FromArgb(rand.Next())), 0, rand.Next(100), 400, rand.Next(100));
				grap.DrawLine(new Pen(Color.FromArgb(rand.Next())), rand.Next(400), 0, rand.Next(400), 100);
			}
			bm.Save(Server.MapPath("~/Content/img/captchaTemp.gif"), System.Drawing.Imaging.ImageFormat.Gif);
			Response.WriteFile(Server.MapPath("~/Content/img/captchaTemp.gif"));
			//grap.DrawString(s: "A", font: System.Drawing.Font.FromHdc, brush: Brushes.Black, point: new PointF(0, 50));
			return null;
		}
		public ActionResult Introduction() {
			return View();
		}
	}
}
